//
//  ClientAppointmentViewController.swift
//  Venus
//
//  Created by qurongxiang on 2018/9/1.
//  Copyright © 2018年 zhaowang. All rights reserved.
//

import UIKit
private let cellID = "cellID"

class ClientAppointmentViewController: BaseViewController {
    
    var modelArr : [SchoolModel] =  [SchoolModel]()
    
    fileprivate lazy var tableView : UITableView = {
        let tableView = UITableView(frame: CGRect.zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(UINib(nibName: "HomeTableViewCell", bundle: nil), forCellReuseIdentifier: cellID)
        return tableView
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        loadData()
    }
    
}

extension ClientAppointmentViewController {
    fileprivate func loadData() {
        self.hudLoad()
        let userModel = UserInfo.shareInstance.userModel()
        let dict:String = ["longitude":userModel.longitude.count == 0 ? "1":userModel.longitude,
                           "latitude":userModel.latitude.count == 0 ? "1":userModel.latitude,
                           "pageNo":"1",
                           "pageSize":"10000"
            ].formatApi()
        
        let range = dict.index(dict.startIndex, offsetBy: 1)..<dict.endIndex
        
        let url = kHost + kqueryAppointmentInstitutionList + "".apiF(time: "1", sign: "2") + "&" + dict[range]
        TempNetTool.post(url, parameters: nil, progress: nil, success: { [weak self](result) in
            self?.hudHide()
            guard let resultDict = result as? [String : NSObject] else { return }
            guard let dataArray = resultDict["data"] as? [[String : NSObject]] else { return }
            if dataArray.count == 0 {
                self?.showEmptyDataView()
                return
            }
            self?.modelArr = SchoolModel.tmSpeelBreakModelArr(dataArray) as! [SchoolModel]
            self?.tableView.reloadData()
        }) { [weak self] (error) in
            self?.hudHide()
            self?.showNetworkErrorView(action: #selector(self?.networkError))
        }
    }
    
    @objc func networkError() {
        loadData()
    }
}

// MARK:- 代理
extension ClientAppointmentViewController:UITableViewDelegate , UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.modelArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView .dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! HomeTableViewCell
        cell.configModel(model: modelArr[indexPath.item], index: indexPath)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 96
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = SchoolDetailViewController()
        let model = modelArr[indexPath.item]
        vc.schoolID = "\(model.id)"
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}

extension ClientAppointmentViewController {
    func setupUI() {
        view.backgroundColor = UIColor.white
        self.title = "我的预约"
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(LayoutCommon.navHeight)
            make.left.bottom.right.equalToSuperview()
        }
    }
}
